using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public class UIGamePanelData : UIPanelData
	{
	}
	public partial class UIGamePanel : UIPanel
	{
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UIGamePanelData ?? new UIGamePanelData();
			// please add init code here

			



			EnemyGenerator.EnemyCount.RegisterWithInitValue(enemcount=>{
				EnemyCount.text="敌人："+enemcount;
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.CurrentSeconds.RegisterWithInitValue(currentSeconds =>
			{
				//每30帧更新一次
				if (Time.frameCount % 30 == 0)
				{
					var currentSecondsInt = Mathf.FloorToInt(currentSeconds);
					var seconds = currentSecondsInt % 60;
					var minutes = currentSecondsInt / 60;
					TimeText.text = $"时间：{minutes:00}:{seconds:00}";
				}

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			//Register：值变化的时候进行回调
			//RegisterWithInitValue：注册的时候就会把值返回一次(注册的时候会执行一次，然后在值变化的时候再进行回调)
			Global.Exp.RegisterWithInitValue(exp =>
			{
				ExpValue.fillAmount=exp/(float)Global.ExpToNextLevel();

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Level.RegisterWithInitValue(level =>
			{
				LevelText.text = "等级：" + level;

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			ExpUpgradePanel.Hide();
			Global.Level.Register((lv) =>
			{
				Time.timeScale = 0;
				ExpUpgradePanel.Show();
				AudioKit.PlaySound("Leveup");

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.Exp.RegisterWithInitValue(exp =>
			{
				if (exp >= Global.ExpToNextLevel())
				{
					Global.Exp.Value -= Global.ExpToNextLevel();
					Global.Level.Value++;
				}

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			

			var enemyyGenerator = FindObjectOfType<EnemyGenerator>();
			ActionKit.OnUpdate.Register(() =>
			{
				Global.CurrentSeconds.Value += Time.deltaTime;

				//坚持3分钟游戏通关
				if (enemyyGenerator.LastWave && enemyyGenerator.CurrenWave == null && EnemyGenerator.EnemyCount.Value == 0)
				{
					this.CloseSelf();
					UIKit.OpenPanel<UIGamePassPanel>();
				}

			}).UnRegisterWhenGameObjectDestroyed(gameObject);

			Global.coin.RegisterWithInitValue(coin=>
			{
				
				Cointext.text="金币 "+coin;

			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnOpen(IUIData uiData = null)
		{
		}

		protected override void OnShow()
		{
		}

		protected override void OnHide()
		{
		}

		protected override void OnClose()
		{
		}
	}
}
